<?php 
#!/usr/local/bin/php

$inputs = ['PPPD-340'];
option($inputs);


// option
function option() {
    if (is_string($inputs)) {
        $inputs = [$inputs];
    }
    foreach ($inputs as $input) {
        $input = trim($input);
        app_run($input);
    }
}

// run api
function app_run($kw='')
{   
    echo "search btytao :".$kw."\n";
    echo "start ... \n";
    $index = 1;
    $html = btytao_http_api($kw);
    $list = get_media_list($html);
    foreach ($list as &$link) {
        echo 'index:'.$index++.'curent:'.$link['title']."\n";
        $content = btytao_http_detail_api($link['link']);
        $link['magnet'] = get_detail($content);
        fmt_magnet($link);
    }
    echo "end ... \n";
}

function stdio_in()
{
    fwrite(STDOUT,"Please input a keyword:");
    $arg = trim(fgets(STDIN));
    return $arg;
}

// format info
function fmt_magnet($info=array(), $filename="d.txt")
{
    $str = "{$info['title']}--{$info['size']}--{$info['create']}\n";
    $str.= $info['magnet']."\n\n";
    file_put_contents($filename, $str, FILE_APPEND);
}

// get media_list content
function get_media_list($subject)
{
    $info_list = array();
    $subject = str_replace(PHP_EOL, '', $subject);
    $pattern = '/<ul class="media-list media-list-set">(.*?)<\/ul>/i';
    preg_match_all($pattern, $subject, $matches);

    foreach ($matches[0] as $value) {
        $info = get_media($value);
        if (empty($info)) {
            continue;
        }
        $info_list[] = $info;
    }
    return $info_list;
}

// get media
function get_media($subject) 
{
    $medias  = array();
    $pattern = '/<span.*?>(.*?)<\/span>/i';
    preg_match_all($pattern, $subject, $matches);
    if (strpos($matches[1][0], 'mail') !== false) {
        array_shift($matches[1]);
    }
    $medias['create'] = $matches[1][1];
    $medias['size']   = $matches[1][2];

    preg_match('/<a href="(.*)" title=".*">.*<\/a>/Ui', $matches[1][3], $links);
    $medias['link']  = $links[1];

    preg_match('/<h4>(.*?)<\/h4>/Ui', $subject, $title);
    $medias['title'] = trim(strip_tags($title[1]));
    return $medias;
}

// get media magnet
function get_detail($subject)
{
    $subject = str_replace(PHP_EOL, '', $subject);
    $pattern = '/<a href="magnet:.*?">(.*?)<\/a>/i';
    preg_match($pattern, $subject, $matches);
    return $matches[1];
}

// get html
function btytao_http_api($kw='')
{
    $api = 'http://www.btytao.com/s.html?kw='.$kw;

    // create a new cURL resource
    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $api);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置为不自动输出结果
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // grab URL and pass it to the browser
    $result = curl_exec($ch);

    // close cURL resource, and free up system resources
    curl_close($ch);

    return $result;
}

// get html
function btytao_http_detail_api($uri='')
{
    $api = 'http://www.btytao.com'.$uri;

    // create a new cURL resource
    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $api);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置为不自动输出结果
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // grab URL and pass it to the browser
    $result = curl_exec($ch);

    // close cURL resource, and free up system resources
    curl_close($ch);

    return $result;
}














